package com.woniuxy.platform.core.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.platform.core.model.MdStore;
import com.woniuxy.platform.core.model.PmMessage;
import com.woniuxy.platform.core.service.MdStoreService;
import com.woniuxy.platform.core.service.PmComplainService;
import com.woniuxy.platform.core.service.PmEvaluateService;
import com.woniuxy.platform.core.service.PmMessageService;
import org.springframework.web.bind.annotation.*;
import com.woniuxy.platform.core.vo.StroreSearchVo;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author gmx
 * @since 2021-07-12
 */
@RestController
@RequestMapping("mdStore")
public class MdStoreController {
    @Resource
    private MdStoreService storeService;
    @Resource
    private PmEvaluateService evaluateService;
    @Resource
    private PmComplainService complainService;
    @Resource
    private PmMessageService messageService;

    /**
     * Description : 查看门店及信誉积分
     * ChangeLog : 1. 创建 (2021/7/12 15:56 [fulang]);
     *
     * @param startpage
     * @param pagesize
     * @return com.github.pagehelper.PageInfo<com.woniuxy.platform.core.model.MdStore>
     */
    @RequestMapping("selectStore/{startpage}/{pagesize}")
    public PageInfo<MdStore> selectStore(@PathVariable int startpage, @PathVariable int pagesize) {
        PageHelper.startPage(startpage, pagesize);
        //初始信誉分为80
        int score = 80;
        List<MdStore> list = storeService.list();
        for (MdStore store : list
        ) {
            int i = complainService.selectComplainCount(store.getStoreId());
            int i1 = evaluateService.selectGood(store.getStoreId());
            int i2 = evaluateService.selectBad(store.getStoreId());
            int score1 = score - i * 5 - i2 + i1;
            store.setCreditScore(score1);
        }
        Page<MdStore> page = (Page<MdStore>) list;
        PageInfo<MdStore> pageInfo = new PageInfo(page);
        return pageInfo;
    }
    /**
     * Description : 警告门店信誉积分小于80分
     * ChangeLog : 1. 创建 (2021/7/12 16:21 [fulang]);
     * @param storeId
     * @return int
    */
    @PostMapping("warning/{storeId}")
    public int warning(@PathVariable Integer storeId){
        PmMessage pmMessage = new PmMessage();
        pmMessage.setCreateTime(new Date());
        pmMessage.setType(1);
        pmMessage.setStoreId(storeId);
        pmMessage.setMessage("警告!您的信誉积分小于80分!");
        int insert = messageService.insert(pmMessage);
        return insert;
    }
    /**
     * Description : 查询门店以往的警告列表
     * ChangeLog : 1. 创建 (2021/7/12 16:38 [fulang]);
     * @param storeId
     * @return java.util.List<com.woniuxy.platform.core.model.PmMessage>
    */
    @GetMapping("selectWarning/{storeId}")
    public List<PmMessage> selectWarning(@PathVariable Integer storeId){
        //根据信息里的警告字段模糊查询警告列表
        List<PmMessage> pmMessages = messageService.selectWarning(storeId);
        return pmMessages;
    }
    /**
     * Description : 根据店家名模糊查询
     * ChangeLog : 1. 创建 (2021/7/13 11:57 [fulang]);
     * @param storeSearchVo
     * @return com.github.pagehelper.PageInfo<com.woniuxy.platform.core.model.MdStore>
    */
    @GetMapping("search")
    public PageInfo<MdStore> search( StroreSearchVo storeSearchVo){

        PageHelper.startPage(storeSearchVo.getPage(),storeSearchVo.getLimit());
        int score = 80;
        List<MdStore> search = storeService.search(storeSearchVo.getStoreName());
        for (MdStore store : search
        ) {
            int i = complainService.selectComplainCount(store.getStoreId());
            int i1 = evaluateService.selectGood(store.getStoreId());
            int i2 = evaluateService.selectBad(store.getStoreId());
            int score1 = score - i * 5 - i2 + i1;
            store.setCreditScore(score1);
        }
        Page<MdStore> pages = (Page<MdStore>) search;
        PageInfo<MdStore> pageInfo = new PageInfo(pages);
        return pageInfo;
    }
    /**
     * Description : 获取商店
     * ChangeLog : 1. 创建 (2021/7/13 16:07 [fulang]);
     *
     * @return java.util.List<com.woniuxy.platform.core.model.MdStore>
    */
    @GetMapping("stores")
    public List<MdStore> stores(){
        List<MdStore> list = storeService.list();
        return list;
    }
}

